package br.com.kliptus.persistence;

import java.util.List;

import org.apache.catalina.User;
import org.maverick.persistence.spring.jpa.PersistenceDefaultDAO;
import org.springframework.stereotype.Repository;

import br.com.kliptus.model.bean.UserJB;
import br.com.kliptus.persistence.dao.UserDAO;

@Repository("userDAOImpl")
public class UserDAOImpl extends PersistenceDefaultDAO<UserJB> implements UserDAO {

	private static final long serialVersionUID = 1L;
	
	public UserDAOImpl() {
		super(UserJB.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserJB getUser(String userName, String password) {
		
		StringBuilder hql = new StringBuilder();
		hql.append(" select user from UserJB user ");
		hql.append(" where user.userName = :userName and user.password = :password ");
		
		List<UserJB> userList = super.entityManager.createQuery(hql.toString())
										    	   .setParameter("userName", userName)
										    	   .setParameter("password", password)
										    	   .getResultList();
		
		return (userList != null && !userList.isEmpty()) ? userList.get(0) : null;
	}

}